package csc.fresher.commons;

import java.util.ArrayList;

public class Pagination {

	private int totalPage;
	private int perPage;
	private int currentPage;
	private ArrayList<PageLink> pagelinks;
	public static final String NEXT_PAGE = "Next Page";
	public static final String PREVIOUS_PAGE = "Previous Page";
	public static final String FIRST_PAGE = "First Page";
	public static final String LAST_PAGE = "Last Page";

	public Pagination(int totalPage, int perPage, int currentPage) {
		if (totalPage < 1) {
			totalPage = 1;
		}
		this.totalPage = totalPage;
		if (perPage < 1) {
			perPage = 1;
		}
		this.perPage = perPage;
		if (currentPage < 1) {
			currentPage = 1;
		}
		this.currentPage = currentPage;
		pagelinks = new ArrayList<PageLink>();
		updatePageLink();
	}

	public void gotoPage(int pageNum) {
		if ((pageNum >= 1) && pageNum <= currentPage) {
			currentPage = pageNum;
			updatePageLink();
		}
	}

	private void updatePageLink() {
		ArrayList<PageLink> tempPagelinks = new ArrayList<PageLink>();
		if (currentPage != 1) {
			tempPagelinks.add(new PageLink(FIRST_PAGE, 1));
			tempPagelinks.add(new PageLink(PREVIOUS_PAGE, currentPage - 1));
		}

		if (totalPage <= 10) {
			for (int i = 1; i <= totalPage; i++) {
				if (i == currentPage) {
					tempPagelinks.add(new PageLink(Integer.toString(i), -1));
				} else {
					tempPagelinks.add(new PageLink(Integer.toString(i), i));
				}
			}
		} else {
			if (currentPage <= 4) {
				for (int i = 1; i <= 6; i++) {
					if (i == currentPage) {
						tempPagelinks.add(new PageLink(Integer.toString(i), -1));
					} else {
						tempPagelinks.add(new PageLink(Integer.toString(i), i));
					}
				}
				tempPagelinks.add(new PageLink("...", 0));
				if (currentPage+10<totalPage) {
					tempPagelinks.add(new PageLink(Integer.toString(currentPage+10),currentPage+10));
				}
				tempPagelinks.add(new PageLink(Integer.toString(totalPage),totalPage));
				
			} else if (currentPage >= totalPage - 3) {
				tempPagelinks.add(new PageLink(Integer.toString(1),1));
				
				if (currentPage-10>1) {
					tempPagelinks.add(new PageLink(Integer.toString(currentPage-10),currentPage-10));
				}

				tempPagelinks.add(new PageLink("...", 0));
				
				for (int i = totalPage-5; i <= totalPage; i++) {
					if (i == currentPage) {
						tempPagelinks.add(new PageLink(Integer.toString(i), -1));
					} else {
						tempPagelinks.add(new PageLink(Integer.toString(i), i));
					}
				}
				

			} else {
				tempPagelinks.add(new PageLink(Integer.toString(1),1));
				
				if (currentPage-10>1) {
					tempPagelinks.add(new PageLink(Integer.toString(currentPage-10),currentPage-10));
				}
				tempPagelinks.add(new PageLink("...", 0));
				
				for (int i = currentPage-2;i<=currentPage+2;i++) {
					if (i == currentPage) {
						tempPagelinks.add(new PageLink(Integer.toString(i), -1));
					} else {
						tempPagelinks.add(new PageLink(Integer.toString(i), i));
					}
				}
				
				tempPagelinks.add(new PageLink("...", 0));
				if (currentPage+10<totalPage) {
					tempPagelinks.add(new PageLink(Integer.toString(currentPage+10),currentPage+10));
				}
				tempPagelinks.add(new PageLink(Integer.toString(totalPage),totalPage));

			}
		}

		if (currentPage != totalPage) {
			tempPagelinks.add(new PageLink(NEXT_PAGE, currentPage + 1));
			tempPagelinks.add(new PageLink(LAST_PAGE, totalPage));

		}
		
		pagelinks = tempPagelinks;
	}

	public ArrayList<PageLink> getPagelinks() {
		return pagelinks;
	}

	public void setPagelinks(ArrayList<PageLink> pagelinks) {
		this.pagelinks = pagelinks;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
		updatePageLink();
	}

	public int getPerPage() {
		return perPage;
	}

	public void setPerPage(int perPage) {
		this.perPage = perPage;
		updatePageLink();
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
		updatePageLink();
	}

}
